<?php

/*
  Project: CI Forum
  Type: PHP and database
  Desc: Contains all functions related to categories.
  Author: Yashpal, Munish, Chander 
 
  Function name : 
  save_forum     -> Insert forum into database
  save_category  -> Insert category into database
  save_thread    -> Insert thread into database
  save_reply     -> Insert reply into database


*/

class Adminusersmodel extends Model 
{

    function Adminusersmodel()
    {
        // Call the Model constructor
        parent::Model();
    }
	
	/*--------------------------------------------------------------------------//
					  		 USER  MANAGEMENT 
	
	//--------------------------------------------------------------------------*/
	function insert_user()
	{	 
		   $insert = array(
                'member_id' => '',
				'member_name' => $this->input->post('member_name'),
				'display_name' => url_title($this->input->post('display_name')),
				'member_password' => $this->input->post('member_password'),
                'member_email' => $this->input->post('member_email'),
                'legends' => $this->input->post('legends'),
				'member_group' => $this->input->post('member_group'),
				'member_photo' => $this->input->post('member_photo'),       //session
				'member_url' => $this->input->post('member_url'),  //session
				'buddy_list' => $this->input->post('buddy_list'),        //url segment
				'ignore_list' => $this->input->post('ignore_list'), 
				'blocked_list' => $this->input->post('blocked_list'),
				'new_pm' =>$this->input->post('new_pm'),
				'aol_im' => $this->input->post('aol_im'),
				'icq' =>$this->input->post('icq'),
				'yahoo_messenger' => $this->input->post('yahoo_messenger'),
				'msn_messenger' => $this->input->post('msn_messenger'),
				'location' => $this->input->post('location'),
				'occupation' =>$this->input->post('occupation'),
				'interests' => $this->input->post('interests'),
				'statistics' => $this->input->post('statistics'),
				'member_thread' => $this->input->post('member_thread'),
				'member_replies' => $this->input->post('member_replies'),
				'local_time' => $this->input->post('local_time'),
				'last_visit' => $this->input->post('last_visit'),
				'join_date' => $this->input->post('join_date'),
				'recent_reply' => $this->input->post('recent_reply'),
				'recent_thread' => $this->input->post('recent_thread'),
				'total_filter' => $this->input->post('total_filter'),
				'total_folders' => $this->input->post('total_folders'),
				'birthday_date' => $this->input->post('birthday_date'),
				'bio' => $this->input->post('bio')
                );
           
		   $check=$this->db->insert('board_members', $insert);
			
			if($check)
				echo"Member is successfully added";
			else
				echo "Error:";	
			
	}
	
	function edit_user($user_id,$page_id)
	{
				 	
		/*	dohash - by default is sha algo 		
		    default user role*/
		//switch case to find proper value
			
			       
			if(trim($this->input->post('password'))=="")
			{ 
					
			
						$update = array(
                			'member_name' => $this->input->post('member_name'),
							'member_group' => $this->input->post('member_group'),
							'member_email' => $this->input->post('member_email'),
							'display_name' => $this->input->post('display_name'),
							'member_url' => $this->input->post('member_url'),
							'member_photo' => $this->input->post('member_photo'),
							'aol_im' => $this->input->post('aol_im'),							
							'icq' => $this->input->post('icq'),
							'yahoo_messenger' => $this->input->post('yahoo_messenger'),
							'msn_messenger' => $this->input->post('msn_messenger'),
							'location' => $this->input->post('location'),
							'occupation' => $this->input->post('occuptaion'),
							'birthday_date' => $this->input->post('birthday_date'),
							'bio' => $this->input->post('bio'),
							'member_group' => $userrole,
						   );
			
			}
			else
			{
						 $update = array(
                			'member_name' => $this->input->post('member_name'),
							'member_group' => $this->input->post('member_group'),
							'member_email' => $this->input->post('member_email'),
							'member_password'=>dohash($this->input->post('password')),
							'display_name' => $this->input->post('display_name'),
							'member_url' => $this->input->post('member_url'),
							'member_photo' => $this->input->post('member_photo'),
							'aol_im' => $this->input->post('aol_im'),							
							'icq' => $this->input->post('icq'),
							'yahoo_messenger' => $this->input->post('yahoo_messenger'),
							'msn_messenger' => $this->input->post('msn_messenger'),
							'location' => $this->input->post('location'),
							'occupation' => $this->input->post('occuptaion'),
							'birthday_date' => $this->input->post('birthday_date'),
							'bio' => $this->input->post('bio'),
							'member_group' => $userrole,
						   );
			
			}
			            
			 

				$this->db->where(array('member_id'=>$user_id));
	            $check=$this->db->update('board_members', $update);
			
				if($check)
				{
					   //User successfully registered and message generated.
					   $info['message']="<p>User successfully updated.<p>";
					   
				}
				else
				{
						//User Registration failed due to unknown error.
						//Most common error is wrong database settings.
						$info['message']="Unexpected Error...Updation Failed.";
						
				}
		  
				$info['title']="Information Page";
				$info['header']="Information";
				$info['url']="/admin/EditUser/".$user_id;
				$info['time']="2";
				
				//$this->load->view('info',$info);
				
            echo "user successfully edited";
     }
	 
	 function RefinePagNav($page_id)
	{
				if($page_id==1)
				$page_id=0;
				
				return $page_id;
	}	
	 
	 function ListAllUsers($pageid)
	{
		$pageid=self::RefinePagNav($pageid);
		
    	$this->db->from('board_members');
	 	$cnt=$this->db->count_all_results();
		
		$config['base_url'] = site_url().'/admin/manage_users/';
		$config['total_rows'] =$cnt;
		$config['per_page'] = '10';
		$config['num_links'] = '3';
		$config['uri_segment'] = '3';

		$this->pagination->initialize($config); 
		$this->db->order_by("member_name"); 
		$query = $this->db->get('board_members',$config['per_page'],$this->uri->segment(3));
		
		
		$out= "<p>".anchor('users/create_thread/'.$this->uri->segment(3),'Create Thread',array("class"=>"btn blue"))."".anchor('admin/','Back to dashboard',array("class"=>"btn blue"))."</p>";
		$out.= "<table width=\"100%\" border=\"1\">";
		$out.="<caption>Members List</caption>
			<thead>
				<tr>
					<th scope=\"col\">Registration Date</th>
					<th scope=\"col\">Member Name</th>
					<th scope=\"col\">Profile</th>
					<th scope=\"col\">Edit</th>
					<th scope=\"col\">Delete</th>
				</tr>
			</thead>	
			<tfoot>
				<tr>
					<th scope=\"row\">Total</th>

					<td colspan=\"4\">$cnt Members</td>
				</tr>
			</tfoot>
			<tbody>";

		foreach ($query->result() as $row)
		{
			$out.=
			 "<tr>
			    <td><abbr title=\"$row->member_name\">".$row->member_name."</abbr></td>
			    <td>".$row->member_name."</td>
			    
				<td>".anchor('users/view_profile/'.$row->member_id,'View')."</td>
			    <td>".anchor('admin/edit_user/'.$row->member_id.'/'.$pageid,'Edit')."</td>
			    <td>".anchor('admin/delete_user/'.$row->member_id.'/'.$pageid,'Delete')."</td>
			  </tr>";
			
			
		} 
		$out.= "</tbody></table>";
		$out.= $this->pagination->create_links();
		return $out;
		
	}
	
	function delete_user($user_id,$page_id)
	{
    $pageid=self::RefinePagNav($page_id);
		$this->db->where('member_id', $user_id);
		$query=$this->db->delete('board_members');
		redirect('/admin/manage_users/'.$pageid, 'refresh');
	}
	
	
	/*--------------------------------------------------------------------------//
					  		 POSTS  MANAGEMENT 
	
	//--------------------------------------------------------------------------*/
	
	//class Postmodel extends Model {

  
	
		

	 //$page_id=self::RefinePagNav($page_id);
	/*function RefinePagNav($page_id)
	{
				if($page_id==1)
				$page_id=0;
				
				return $page_id;
	}	*/
	
	
	function Deletepost($post_id,$pageid)
	{
    $pageid=self::RefinePagNav($pageid);
		$this->db->where('post_id', $post_id);
		$query=$this->db->delete('posts');
		
		//redirect('/admin/ListUsers/'.$pageid, 'refresh');
	}
	

	
	 //Function Name - UpdatePosts
	//This function updates a post.
	//Parram - postid and pageID
	//output - Info about updation
	
	function Updatepost($post_id,$page_id)
	{
		 	
							
				if($this->input->post('enable_emoticons'))
				$enable_emt = 1;
				else
			 	$enable_emt = 0;
				if($this->input->post('enable_signature'))
				$enable_sgn = 1;
				else
			 	$enable_sgn = 0;
				if($this->input->post('enable_notification'))
				$enable_ntfn = 1;
				else
			 	$enable_ntfn = 0;
				
				//GETTING TIME AND AUTHOR NAME OF POST
				$now= time();
				$datestring = " %d /%m /%y ";
				$dte = mdate($datestring, $now);
				echo $dte;
				$this->db->select('author_name');
				$this->db->where('post_id', $post_id);
				$query = $this->db->get('posts');
				$name=$query->row_array();			
				$reason = $this->input->post('reason_content');
				$reason = $reason ." by ". $name['author_name'] ." at  ". $dte ;
								
						
						$update = array(
                			'post_Content' => $this->input->post('post_content'),
							//'author_name' => $this->input->post('author_name'),
							'enable_emoticons' => $enable_emt,
							'enable_signature' => $enable_sgn,
							'enable_notification' => $enable_ntfn,
							'reason_content' => $reason,
							'icon_id'=>$this->input->post('icon_id'),
						   );
				            
			 

				$this->db->where(array('post_id'=>$post_id));
	            $check=$this->db->update('posts', $update);
			
				if($check)
				{
					   //User successfully registered and message generated.
					   $info['message']="<p>Post successfully updated.<p>";
				}
				else
				{
						//User Registration failed due to unknown error.
						//Most common error is wrong database settings.
						$info['message']="Unexpected Error...Updation Failed.";
						
				}
		  
				$info['title']="Information Page";
				$info['header']="Information";
				$info['url']="/admin/EditUser/".$post_id;
				$info['time']="2";
				
				//$this->load->view('info',$info);
				
            
    }
	
 	function list_all_posts($pageid)
	{
		$pageid=self::RefinePagNav($pageid);
		
	$this->db->from('posts');
	 	$cnt=$this->db->count_all_results();
		
		$config['base_url'] = site_url().'/admin/ListUsers/';
		$config['total_rows'] =$cnt;
		$config['per_page'] = '10';
		$config['num_links'] = '3';
		$config['uri_segment'] = '3';


		$this->pagination->initialize($config); 
		$this->db->order_by("posted_date", "desc"); 
		$query = $this->db->get('posts',$config['per_page'],$this->uri->segment(3));
		
		$out= "<table width=\"100%\" border=\"0\">";
		$out.="<caption>Posts List</caption>
			<thead>
				<tr>
					<th scope=\"col\">Posted On</th>
					<th scope=\"col\">Member Name</th>
					<th scope=\"col\">Edit</th>
					<th scope=\"col\">Delete</th>
				</tr>
			</thead>	
			<tfoot>
				<tr>
					<th scope=\"row\">Total</th>

					<td colspan=\"5\">$cnt Posts</td>
				</tr>
			</tfoot>
			<tbody>";

		foreach ($query->result() as $row)
		{
			$out.=
			 "<tr>
			    <td><abbr title=\"$row->posted_date\">".substr($row->posted_date,0,10)."</abbr></td>
			    <td>".$row->author_name."</td>
			    <td>";
						
						
				$out.="</td>
				
			    <td>".anchor('admin/editpost/'.$row->post_id,'Edit')."</td>
			    <td>".anchor('admin/deletepost/'.$row->post_id.'/'.$pageid,'Delete')."</td>
			  </tr>";
			
			
		} 
		$out.= "</tbody></table>";
		$out.= $this->pagination->create_links();
		return $out;
		
	}

	


	
	
	
	
}
	
	
//}
?>